﻿// (c) Copyright Clovis Coli Jr.
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.

using System;
using System.Windows;

namespace BrasilFramework.Controle.WPF.Comum.Validacao
{
    public class LengthValidator : ValidatorBase
	{
        public int MinLength
		{
			get { return (int)GetValue(MinLengthProperty); }
			set { SetValue(MinLengthProperty, value); }
		}

        public static readonly DependencyProperty MinLengthProperty =
			DependencyProperty.Register("MinLength", typeof(int), typeof(LengthValidator), new PropertyMetadata(Int32.MinValue));
		
		public int MaxLength
		{
			get { return (int)GetValue(MaxLengthProperty); }
			set { SetValue(MaxLengthProperty, value); }
		}

        public static readonly DependencyProperty MaxLengthProperty =
			DependencyProperty.Register("MaxLength", typeof(int), typeof(LengthValidator), new PropertyMetadata(Int32.MaxValue));

		protected override bool ValidateControl()
		{
            if ( ElementToValidate is System.Windows.Controls.TextBox)
			{
				var box = ElementToValidate as System.Windows.Controls.TextBox;
                return box.Text.Length >= MinLength && box.Text.Length <= MaxLength;
			}
            return true;
		}
	}
}
